#include "formkmxx.h"
#include "ui_formkmxx.h"
#include <mysql.h>
#include <QClipboard>

Formkmxx::Formkmxx(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Formkmxx)
{
    ui->setupUi(this);
    this->setWindowTitle("科目代码设置");
    this->show();
    ui->cmbHSFS->clear();
    ui->cmbHSFS->addItem("科目核算");
    ui->cmbHSFS->addItem("项目核算");
    ui->cmbHSHB->clear();
    ui->cmbHSHB->addItem("本币");

    QString SQL="";
    QString ND=mySQL::ND;
    SQL=QString("SELECT xmdm,xmmc FROM `hsxm` WHERE type_id='%1' and nd='%2' and length(xmdm)=1 order by xmdm").arg(mySQL::TypeId).arg(ND);
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    query.exec(SQL);
    ui->treeWidgetXM->clear();
    ui->treeWidgetXM->setHeaderLabels(QStringList() << "项目编号" << "项目名称" );
    ui->treeWidgetXM->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度
    while(query.next()){
        QStringList child;
        child.clear();
        child << query.value("xmdm").toString();
        child << query.value("xmmc").toString();
        QTreeWidgetItem * citem =new QTreeWidgetItem(child);
        citem->setCheckState (0, Qt::Unchecked);
        ui->treeWidgetXM->addTopLevelItem(citem);
    }
    SQL=QString("SELECT lx,hbmc,hbjc,hl FROM `hl` WHERE type_id='%1' and nd='%2' order by lx desc ").arg(mySQL::TypeId).arg(ND) ;
    query.exec(SQL);
    while(query.next()){
       ui->cmbHSHB->addItem(query.value("hbjc").toString());
    }
    ui->btnXMXZ->setText("重新勾选");
    ui->treeWidgetXM->setVisible(false);
    ui->cmbJDFX->clear();
    ui->cmbJDFX->addItem("借方");
    ui->cmbJDFX->addItem("贷方");    
    SQL="SELECT nd FROM  nd ";
    SQL+=" order by nd desc ";
    ui->cmbND->clear();
    query.exec(SQL);
    while(query.next()){
        ui->cmbND->addItem(query.value("nd").toString());
    }
    query.clear();
    RefTree();
}

Formkmxx::~Formkmxx()
{
    delete ui;
}

void Formkmxx::RefTree()
{

    ui->treeWidget->clear();
    ui->treeWidget->setHeaderLabels(QStringList() << "科目代码" << "科目名称" << "核算方式" <<"项目编号" <<"余额方向" <<"核算货币"  );
    ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度
    //ui->treeWidget->setStyleSheet("QTreeWidget::item{border-right: 1px solid blue;border-left: 1px solid blue;border-bottom: 1px solid blue;}"); //加表格线
    QString SQL="";
    QString ND=mySQL::ND;
    QString KMDM=ui->txtKMDM->text().trimmed();
    QStringList child;
    SQL=QString("SELECT kmdm,kmmc,hsfs,ye,xmbh,hshb FROM `kmxx` WHERE type_id='%1' and nd='%2' order by kmdm").arg(mySQL::TypeId).arg(ND);
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("kmdm").toString().trimmed();
         child.clear();
         child << query.value("kmdm").toString();
         child << query.value("kmmc").toString();
         child << query.value("hsfs").toString();
         child << query.value("xmbh").toString();
         child << query.value("ye").toString();
         child << query.value("hshb").toString();
         QTreeWidgetItem * citem =new QTreeWidgetItem(child);
         if (str.length() ==1){
             ui->treeWidget->addTopLevelItem(citem);
         }else{
             //遍历找出父节点
             bool f=false;
             QTreeWidgetItemIterator it(ui->treeWidget);
                 while (*it) {
                      //(*it)->text(0);
                     if ((*it)->text(0)==str.left(str.length()-1)){
                         (*it)->addChild(citem);
                         f=true;
                     }
                     ++it;
                 }
                 if(f==false){
                     //如果减一个字符没照到上级 就减2个字符
                     QTreeWidgetItemIterator it(ui->treeWidget);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-2)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
                 if(f==false){
                     //如果减2个字符没照到上级 就减3个字符
                     QTreeWidgetItemIterator it(ui->treeWidget);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-3)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
         }
         if (KMDM!=""){
             QTreeWidgetItemIterator it(ui->treeWidget);
             int itemCount=0;
             while (*it) {
                  //(*it)->text(0);
                 if  ((*it)->text(0).left(KMDM.length())==KMDM){
                     itemCount++;
                 }
                 if  (KMDM.left((*it)->text(0).length())==(*it)->text(0)){
                     (*it)->setExpanded(true);
                 }else{
                     (*it)->setExpanded(false);
                 }
                 if ((*it)->text(0)==KMDM){
                     (*it)->setSelected(true);
                     ui->treeWidget->scrollToItem((*it));
                     str=(*it)->text(1);
                     ;
                 }else{
                     (*it)->setSelected(false);
                     //
                 }
                 ++it;
             }
         }
    }
}
void Formkmxx::on_btnSave_clicked()
{
    QString SQL="";
    QString KMDM="";
    QString KMMC="";
    QString ND=mySQL::ND;
    QString HSFS="";
    QString JDFX="";
    QString XMBH="";
    QString HSHB="";
    int Idd=0;
    bool Ssign=false;
    bool Tsign=false;
    KMDM=ui->txtKMDM->text().trimmed();
    KMMC=ui->txtKMMC->text().trimmed();
    JDFX=ui->cmbJDFX->currentText().trimmed();
    HSFS=ui->cmbHSFS->currentText();
    XMBH=ui->txtHSXM->text().trimmed();
    HSHB=ui->cmbHSHB->currentText();
    if (KMDM==""){
        QMessageBox::warning(this,"警告","科目代码为空！");
        return;
    }
    //QMessageBox::warning(this,"警告","继续保存！");
    //先查找一下是否存在这个科目代码
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT idd FROM `kmxx` WHERE kmdm='%1' and type_id='%2' and nd='%3'").arg(KMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
    //QMessageBox::warning(this,"警告",SQL);
    //qDebug() <<SQL;
    query.exec(SQL);
    Ssign=false;
    while(query.next()){
        Ssign=true;
        Idd=query.value("idd").toInt();
        //QMessageBox::warning(this,"警告","idd="+Idd);
    }
    Tsign=true;
    if (Ssign==false && KMDM.length()>1){
        //新建 并 科目代码长度小于1  就判断是否存在上级科目
        int kl=KMDM.length()-1;
        SQL=QString("SELECT idd FROM `kmxx` WHERE  type_id='%1' and nd='%2' and (kmdm='%3' ").arg(mySQL::TypeId).arg(ND.trimmed()).arg(KMDM.left(kl).trimmed());
        if (kl>2){
            //兼容2位科目代码

            SQL+=QString(" or kmdm='%1'").arg(KMDM.left(kl-1));
            SQL+=QString(" or kmdm='%1'").arg(KMDM.left(kl-2));
        }

        SQL+= ")";
        query.exec(SQL);
        Tsign=false;
        while(query.next()){
            Tsign=true;
        }
    }
    if (Tsign==true){
        //开始准备保存科目代码
        SQL="";
        if (Ssign==false ){
            SQL="INSERT INTO kmxx(type_id,nd,kmdm,kmmc,ye,xmbh,hshb,hsfs) VALUES (";
            SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(KMDM.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(KMMC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JDFX.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(XMBH.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(HSHB.trimmed().replace("'","''"));
            SQL+=QString("'%1')").arg(HSFS.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }else{
            SQL="update kmxx set ";
            SQL+=QString("ND='%1',").arg(ND.trimmed().replace("'","''"));
            SQL+=QString("kmdm='%1',").arg(KMDM.trimmed().replace("'","''"));
            SQL+=QString("kmmc='%1',").arg(KMMC.trimmed().replace("'","''"));
            SQL+=QString("ye='%1',").arg(JDFX.trimmed().replace("'","''"));
            SQL+=QString("xmbh='%1',").arg(XMBH.trimmed().replace("'","''"));
            SQL+=QString("hshb='%1',").arg(HSHB.trimmed().replace("'","''"));
            SQL+=QString("hsfs='%1' ").arg(HSFS.trimmed().replace("'","''"));            
            SQL+=QString("where idd=%1").arg(Idd);
            //QMessageBox::warning(this,"警告",SQL);
        }
        if (SQL!=""){
            query.exec(SQL);            
            RefTree();
            QMessageBox::information(this,"信息","保存成功！");
        }

    }else{
        QMessageBox::warning(this,"警告","不存在上级科目 无法保存！！");
    }

}
void Formkmxx::on_btnClose_clicked()
{
    this->close();
}

void Formkmxx::on_btnPaste_clicked()
{

    QMessageBox message(QMessageBox::Warning,"请确认","确认粘贴数据的格式为 \n[科目代码]，[科目名称]?",QMessageBox::Yes|QMessageBox::No,NULL);
        if (message.exec()==QMessageBox::Yes)
        {
            QClipboard *board = QApplication::clipboard();
            QString str = board->text();
            //QMessageBox::information(NULL, "From clipboard", str);
            QStringList strList = str.split("\n");
            QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
            for(int i = 0; i < strList.size()-1; i++) {
                QStringList subList = strList[i].split("\t");
                QString SQL="";
                QString KMDM=subList[0].trimmed();
                QString KMMC=subList[1].trimmed();
                QString ND=mySQL::ND;
                QString HSFS="科目核算";
                QString XMBH="";
                QString HSHB="本币";
                QString YE="";
                if (KMDM.left(1)=="1"){
                    YE="借方";
                }else{
                    YE="贷方";
                }
                //qDebug()<<subList[0]+"-"+subList[1];
                SQL="INSERT INTO kmxx(type_id,nd,kmdm,kmmc,ye,xmbh,hshb,hsfs) VALUES (";
                SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(KMDM.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(KMMC.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(YE.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(XMBH.trimmed().replace("'","''"));
                SQL+=QString("'%1',").arg(HSHB.trimmed().replace("'","''"));
                SQL+=QString("'%1')").arg(HSFS.trimmed().replace("'","''"));
                //qDebug()<<SQL;
                query.exec(SQL);
            }
            RefTree();
            //QMessageBox::information(NULL, "From clipboard", str);
        }
        else
        {
            qDebug()<<"clicked no\n";
        }


}

void Formkmxx::on_btnRefresh_clicked()
{
    RefTree();
}



void Formkmxx::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    QString HSXM=item->text(3);
    QTreeWidgetItemIterator it(ui->treeWidgetXM);
    while (*it) {
         //(*it)->text(0);
        (*it)->setCheckState (0, Qt::Unchecked);
        QString str=(*it)->text(0);
        if (HSXM.contains(str,Qt::CaseInsensitive)) { //是否包含字符
            (*it)->setCheckState (0, Qt::Checked);
        }

        ++it;
    }

    ui->txtKMDM->setText(item->text(0));
    ui->txtKMMC->setText(item->text(1));
    ui->cmbHSFS->setCurrentText(item->text(2));
    ui->cmbJDFX->setCurrentText(item->text(4));
    ui->txtHSXM->setText(item->text(3));
    ui->cmbHSHB->setCurrentText(item->text(5));
    int i=column;
    i++;
}

void Formkmxx::on_btnChild_clicked()
{
    QString SQL="";
    QString KMDM="";
    QString KMMC="";
    QString ND=mySQL::ND;
    QString HSFS="";
    QString JDFX="";
    QString XMBH="";
    QString HSHB="";
    HSHB=ui->cmbHSHB->currentText();
    XMBH=ui->txtHSXM->text().trimmed();
    JDFX=ui->cmbJDFX->currentText().trimmed();
    KMDM=ui->txtKMDM->text().trimmed();
    KMMC=ui->txtKMMC->text().trimmed();
    HSFS=ui->cmbHSFS->currentText();
    if (KMDM==""){
        QMessageBox::warning(this,"警告","科目代码为空！");
        return;
    }
    //QMessageBox::warning(this,"警告","继续保存！");
    //先查找一下是否存在这个科目代码
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT max(kmdm) as t  FROM `kmxx` WHERE (kmdm='%1' or kmdm like '%1_') and type_id='%2' and nd='%3'").arg(KMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
    //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("t").toString().trimmed();
        //QMessageBox::warning(this,"警告",str);
        if (str==""){
            QMessageBox::warning(this,"警告","未发现项目的存在，请重新选择！");
            return;
        }
        if (str==KMDM){
            //如果没有下级科目就添加第一个项目
            KMDM+="0";
        }else{
            //有下级科目的情况下取出最大看值
            QString MaxKM=str.right(1).toUtf8();
            int K_ascii ;
            QVector<uint>v=MaxKM.toUcs4();
                foreach(int a,v)
                {
                    qDebug()<<"Ascii:"<<a;
                    K_ascii=a;
                }
                //自动编号的次序为： 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ 到Z后将无法编号
                if (K_ascii>=90){
                    QMessageBox::warning(this,"警告","编号已经到了Z了无法再继续了！");
                    return;
                }
                if (K_ascii<57){
                    K_ascii++;
                }else{
                    if (K_ascii==57){
                        K_ascii=65;
                    }else{
                        K_ascii++;
                    }
                }
                KMDM+=(QChar)K_ascii;
        }
    }
    KMMC="新编科目";
    SQL="INSERT INTO kmxx(type_id,nd,kmdm,kmmc,ye,xmbh,hshb,hsfs) VALUES (";
    SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(KMDM.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(KMMC.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(JDFX.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMBH.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(HSHB.trimmed().replace("'","''"));
    SQL+=QString("'%1')").arg(HSFS.trimmed().replace("'","''"));
    query.exec(SQL);
    RefTree();
    ui->txtKMDM->setText(KMDM);
    ui->txtKMMC->setText(KMMC);
    QMessageBox::information(this,"提醒","新科目代码为："+KMDM);
}

void Formkmxx::on_btnJuxtaposition_clicked()
{
    QString SQL="";
    QString KMDM="";
    QString KMMC="";
    QString ND=mySQL::ND;
    QString HSFS="";
    QString JDFX="";
    QString XMBH="";
    QString HSHB="";
    HSHB=ui->cmbHSHB->currentText();
    XMBH=ui->txtHSXM->text().trimmed();
    JDFX=ui->cmbJDFX->currentText().trimmed();
    KMDM=ui->txtKMDM->text().trimmed();
    KMDM=KMDM.left(KMDM.length()-1);
    KMMC=ui->txtKMMC->text().trimmed();
    HSFS=ui->cmbHSFS->currentText();
    if (KMDM==""){
        QMessageBox::warning(this,"警告","科目代码为空！");
        return;
    }
    //QMessageBox::warning(this,"警告","继续保存！");
    //先查找一下是否存在这个科目代码
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT max(kmdm) as t  FROM `kmxx` WHERE kmdm like '%1_' and type_id='%2' and nd='%3' ").arg(KMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
    //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("t").toString().trimmed();
        //QMessageBox::warning(this,"警告",str);
        if (str==""){
            QMessageBox::warning(this,"警告","未发现项目的存在，请重新选择！");
            return;
        }
        if (str==KMDM){
            //如果没有下级科目就添加第一个项目
            KMDM+="0";
        }else{
            //有下级科目的情况下取出最大看值
            QString MaxKM=str.right(1).toUtf8();
            int K_ascii ;
            QVector<uint>v=MaxKM.toUcs4();
                foreach(int a,v)
                {
                    qDebug()<<"Ascii:"<<a;
                    K_ascii=a;
                }
                //自动编号的次序为： 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ 到Z后将无法编号
                if (K_ascii>=90){
                    QMessageBox::warning(this,"警告","编号已经到了Z了无法再继续了！");
                    return;
                }
                if (K_ascii<57){
                    K_ascii++;
                }else{
                    if (K_ascii==57){
                        K_ascii=65;
                    }else{
                        K_ascii++;
                    }
                }
                KMDM+=(QChar)K_ascii;
        }
    }

    KMMC="新编科目";
    SQL="INSERT INTO kmxx(type_id,nd,kmdm,kmmc,ye,xmbh,hshb,hsfs) VALUES (";
    SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(KMDM.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(KMMC.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(JDFX.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(XMBH.trimmed().replace("'","''"));
    SQL+=QString("'%1',").arg(HSHB.trimmed().replace("'","''"));
    SQL+=QString("'%1')").arg(HSFS.trimmed().replace("'","''"));
    query.exec(SQL);
    RefTree();

    ui->txtKMDM->setText(KMDM);
    ui->txtKMMC->setText(KMMC);
    QMessageBox::information(this,"提醒","新科目代码为："+KMDM);
}

void Formkmxx::on_btnDel_clicked()
{
    QString SQL="";
    QString KMDM="";
    QString KMMC="";
    QString ND=mySQL::ND;
    QString HSFS="";
    bool delft=false;
    KMDM=ui->txtKMDM->text().trimmed();
    KMMC=ui->txtKMMC->text().trimmed();
    HSFS=ui->cmbHSFS->currentText();
    SQL="确认删除这条记录吗？ 删除后是无法恢复的！！！！";
    if (QMessageBox::information(NULL, "提醒", SQL,QMessageBox::Yes | QMessageBox::No, QMessageBox::No)==QMessageBox::No){
        return;
    }
    SQL="";
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT count(kmdm) as t  FROM `kmxx` WHERE kmdm like '%1%' and type_id='%2' and nd='%3' ").arg(KMDM.trimmed()).arg(mySQL::TypeId).arg(ND.trimmed());
    //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    SQL="";
    while(query.next()){
        if (query.value("t").toString().trimmed()=="1"){

            delft=true;
        }else{
            QMessageBox::information(this,"提醒","存在下级科目，不得删除！！");
            return;
        }

    }
    if (delft==true){
        SQL=QString("delete  FROM `kmxx` WHERE kmdm = '%1' and type_id='%2' and nd='%3'").arg(KMDM.trimmed()).arg(mySQL::TypeId).arg(ND);
        query.exec(SQL);
        RefTree();
        QMessageBox::information(this,"提醒","删除成功");
    }
}



void Formkmxx::on_cmbHSFS_currentIndexChanged(int index)
{
    if (index==0){
        ui->txtHSXM->setText("");
        //ui->txtHSXM->setEnabled(false);
        ui->txtHSXM->setVisible(false);
        ui->txtHSXM->setEnabled(false);
        ui->btnXMXZ->setVisible(false);
        ui->treeWidgetXM->setVisible(false);
    }else{
        //ui->txtHSXM->setEnabled(true);
        ui->txtHSXM->setVisible(true);
        ui->btnXMXZ->setVisible(true);
        ui->txtHSXM->setEnabled(false);
    }
}

void Formkmxx::on_btnXMXZ_clicked()
{
    if (ui->btnXMXZ->text()=="重新勾选"){
        ui->btnXMXZ->setText("取消勾选");
        ui->txtHSXM->setText("");
        ui->treeWidgetXM->setVisible(true);
    }else{
        ui->btnXMXZ->setText("重新勾选");
        ui->treeWidgetXM->setVisible(false);

    }
}



void Formkmxx::on_treeWidgetXM_itemClicked(QTreeWidgetItem *item, int column)
{
    if (mySQL::SendString==""){
        mySQL::SendString="wait";
        QString tmpStr=item->text(0);
        tmpStr+=QString("%1").arg(column);
        QTreeWidgetItemIterator it(ui->treeWidgetXM);
        QString str="";
            while (*it) {
                 //(*it)->text(0);
                if ((*it)->checkState(0)==Qt::Checked){
                    str+="";
                    str+=(*it)->text(0);
                    str+=".";
                }
                ++it;
            }
            if (str!=""){
                str=str.left(str.length()-1);
            }

            ui->txtHSXM->setText(str);
            mySQL::SendString="";
    }
}


void Formkmxx::on_btnIMP_clicked()
{
    QString SQL="";
    QString KMDM="";
    QString KMMC="";
    QString ND=mySQL::ND;
    QString HSFS="";
    QString JDFX="";
    QString XMBH="";
    QString HSHB="";
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("DELETE FROM `kmxx` WHERE type_id='%1' and nd='%2'").arg(mySQL::TypeId).arg(ND);
    query.exec(SQL);
     //导入科目代码
    SQL=QString("SELECT kmdm,kmmc,hsfs,ye,xmbh,hshb FROM `kmxx` WHERE type_id='%1' and nd='%2' order by kmdm").arg(mySQL::TypeId).arg(ui->cmbND->currentText());
    query.exec(SQL);
    while(query.next()){
        KMDM=query.value("kmdm").toString().trimmed();
        KMMC=query.value("kmmc").toString();
        HSFS=query.value("hsfs").toString();
        XMBH=query.value("xmbh").toString();
        JDFX=query.value("ye").toString();
        HSHB=query.value("hshb").toString();
        SQL="INSERT INTO kmxx(type_id,nd,kmdm,kmmc,ye,xmbh,hshb,hsfs) VALUES (";
        SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(ND.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(KMDM.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(KMMC.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(JDFX.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(XMBH.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(HSHB.trimmed().replace("'","''"));
        SQL+=QString("'%1')").arg(HSFS.trimmed().replace("'","''"));
        mySQL::cn.exec(SQL);
    }
   RefTree();

}
